package com.luoran.bartalks.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.luoran.bartalks.dao.IBtRuleRecordDao;
import com.luoran.bartalks.dao.IBtRuleRecordPicDao;
import com.luoran.bartalks.entity.BaseInfo;
import com.luoran.bartalks.entity.biz.BtRuleRecord;
import com.luoran.bartalks.entity.biz.BtRuleRecordPic;
import com.luoran.bartalks.service.IBtRuleRecordService;

@Transactional
@Service
public class BtRuleRecordService implements IBtRuleRecordService {

    @Autowired
    private IBtRuleRecordDao ruleRecordDao;

    @Autowired
    private IBtRuleRecordPicDao ruleRecordPicDao;
    
    @Override
	public PageQuery<? extends BaseInfo> getQueryList(PageQuery<? extends BaseInfo> pageQuery) {
    	ruleRecordDao.queryMaterialruleRecordPage(pageQuery);
		return pageQuery;
	}

    // 保存打尺记录
    @Override
    public void saveRecord(BtRuleRecord ruleRecord, String picUrl) {
        try {
            // 保存打尺记录
            ruleRecordDao.insertTemplate(ruleRecord);
            if ("".equals(picUrl)) {//未拍照上传,保存打尺记录执行
               /* BtRuleRecordPic ruleRecordPic = new BtRuleRecordPic();
                ruleRecordPic.setRuleRecordId(ruleRecord.getId());
                ruleRecordPic.setRulePicUrl("/bartalks/not-photo.png");
                ruleRecordPicDao.insertTemplate(ruleRecordPic);*/
            } else {// 拍照上传,保存打尺记录执行
                List<BtRuleRecordPic> pics = new ArrayList<>();
                String[] arrPicUrl = picUrl.split("#");// 接收打尺拍照图片url字符串,
                for (String url : arrPicUrl) {
                    BtRuleRecordPic ruleRecordPic = new BtRuleRecordPic();
                    ruleRecordPic.setRuleRecordId(ruleRecord.getId());
                    ruleRecordPic.setRulePicUrl(url);
                    pics.add(ruleRecordPic);
                }
                ruleRecordPicDao.insertBatch(pics);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<BtRuleRecord> getRuleRecord(String id) {
        return ruleRecordDao.getRuleRecord(id);
    }


}
